/**
 * 
 */
package com.amoby.main.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.amoby.base.common.Constants;
import com.amoby.base.utils.DateUtil;
import com.amoby.main.entity.AlipayOrder;
import com.amoby.main.entity.Business;
import com.amoby.main.entity.Cbaacc;
import com.amoby.main.entity.Cbabill;
import com.amoby.main.entity.CommbankPay;
import com.amoby.main.entity.SysConfig;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.mapper.AlipayOrderMapper;
import com.amoby.main.mapper.CbaaccMapper;
import com.amoby.main.mapper.CbabillMapper;
import com.amoby.main.mapper.PushSmsLogMapper;
import com.amoby.main.mapper.VerifierMapper;
import com.amoby.main.service.BillOperation;
import com.amoby.main.service.CbaBillService;
import com.amoby.main.service.CommBankService;
import com.amoby.main.service.ConfigService;
import com.amoby.main.service.PushSmsService;
import com.amoby.main.service.SmsService;


/**
 * 
 * @date 2015-6-24 下午1:37:52
 * @author zhaoyy
 * @version V1.0
 * @Description
 * 
 */
@Service("cbaBillService")
public class CbaBillServiceImpl implements CbaBillService {
    private static final Logger   logger               = LoggerFactory.getLogger(CbaBillServiceImpl.class);
    /** CBA配置ID */
    private static final String   CFG_CBA_CFG_ID       = "CBA_CFG";
    /** CBA 自动转账限额配置KEY */
    private static final String   CFG_AUTO_PAY_LMT_KEY = "AUTO_PAY_LMT";
    /** CBA 最低转账限额配置KEY */
    private static final String   CFG_BATCH_LMT_KEY    = "BATCH_LMT";
    /** CBA 转账池限额配置KEY */
    private static final String   CFG_TRANSFER_POOL_LMT_KEY    = "TRANSFER_POOL_LMT";
    @Autowired
    private ConfigService            iConfigMgr;
    @Autowired
    private CbabillMapper         cbabillMapper;
    @Autowired
    private CbaaccMapper          cbaaccMapper;
    @Autowired
    private CommBankService       commBankService;
    @Autowired
    private VerifierMapper        verifierMapper;
    @Autowired
    private SmsService            smsService;
    @Autowired
    private AlipayOrderMapper     alipayOrderMapper;
    @Autowired
    private PushSmsService        pushSmsService;
    @Autowired
    private PushSmsLogMapper         pushSmsLogMapper;

    public CbaBillServiceImpl() {
    }
   

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public Cbabill flowBill(Long billId, BillOperation billOperation) throws Exception {
        logger.debug("联邦银行向商家转账开始==========================================");
        // 为了保证每一笔订单的数据完整性，必须保证线程同步， 在取数据时使用悲观锁（select * from tb_cbabill where id = ? for update），提交后下一个事务才能进行操作
        Cbabill cbabill = cbabillMapper.selectByIdForUpdate(billId);
        //Byte bsType = cbabill.getBsType();
        // 流转状态
        cbabill = billOperation.flow(cbabill);
        cbabill.setUptime(DateUtil.getCurDate());
        byte state = cbabill.getState();
        Byte payType = cbabill.getPayType();
        if (state == 4) {
            // 账单流程结束
            cbabill.setDoneTime(DateUtil.getCurDate());
        }
        // 更新到数据库
        if (cbabillMapper.updateByPrimaryKeySelective(cbabill) != 1) {
            throw new RuntimeException("save fail");
        }
        if (payType == 1 || payType == 2) { // 如果非人工转账账单， 则需要进行如下操作
            if (state == 3) {
                // 向联邦银行提交转账请求
                CommbankPay commbankPay = commBankService.createPayment(cbabill);
                commBankService.postPayment(commbankPay);
            }
        }
        if (state == 4) {// 转账成功后给对应商家发送短信1.7.0版本需求    
            //if (bsType == 5) {
            logger.debug("联邦银行转成功向支付宝商家发送短信 billid=" + cbabill.getId());
            //smsAlipayBusiness(cbabill);
            //转账成功后向app推送消息，然后app给商家发送短信
            List<AlipayOrder> orders = alipayOrderMapper.selectAlipayOrderByBillId(cbabill.getId());
            logger.debug("需要转账的订单======" + orders.size() + "=========");
            Business business = cbabillMapper.getAlipayBusinessByBill(cbabill.getId());
            logger.debug("向商家发送短信开始============"+(business != null && orders!=null && orders.size()>0));
            if (business != null && orders!=null && orders.size()>0) {
                /** 向商家发送短信 */
                if (business.getPhone2() != null && !"".equals(business.getPhone2()) && !"".equals(business.getShopName())
                        && business.getShopName() != null) {
                    // 修改转账短信推送信息
                    for (int i = 0; i < orders.size(); i++) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("orderNo", orders.get(i).getOrderNo());
                        map.put("billId", cbabill.getId());
                        map.put("pushtCount", 1);   //转账推送次数
                        map.put("transferpushSendTime", DateUtil.getDatetime());
                        // 根据订单号修改短信日志信息
                        logger.debug("根据订单号修改短信日志信息======" + orders.size() + "=========" + orders.get(i).getOrderNo());
                        pushSmsLogMapper.updBillIdAndTstimeByOrderNo(map);
                    }
                    String content = "[" + business.getShopName() + "]" + " Please be advised you have AU $" + cbabill.getAmount()
                            + " through CBA transfer to your,please check. http://mmobile.supaytech.com.au/ . " + Constants.SMS_LAST_STR;
                    StringBuffer sb = new StringBuffer(business.getPhone2()+"");
                    String sendPhone = "";
                    if (StringUtils.isNotEmpty(business.getPhone3())) {// 大陆商家
                        sb.append("|").append(business.getPhone3());
                        if (StringUtils.isNotEmpty(business.getPhone4())) {
                            sb.append("|").append(business.getPhone4());
                        }
                        if (StringUtils.isNotEmpty(business.getPhone5())) {
                            sb.append("|").append(business.getPhone5());
                        }
                    }
                    sendPhone = sb.toString();
                    logger.debug("转账第一次需要推送的号码===="+sendPhone);
                    pushSmsService.smspush(sendPhone, String.valueOf(cbabill.getId()),content,2);
                    // =================== 上报动态定时任务开始 ============================
                    /*Map<String, Object> restult = pushSmsService.pushsmsTask(String.valueOf(cbabill.getId()),String.valueOf(new Date().getTime()),2,cbabill.getAmount());
                    if (!"success".equals(restult.get("r"))) {  //如果上报失败则再次上报
                        pushSmsService.pushsmsTask(String.valueOf(cbabill.getId()),String.valueOf(new Date().getTime()),2,cbabill.getAmount());
                    }*/
                    // =================== 上报动态定时任务 结束============================
                }
            }
            //}
        }
        logger.info("账单状态完成流转 billOperation={}", billOperation);
        return cbabill;
    }

    /**
     * 处理定时转账
     * 
     * @param paySchedu
     */
    @Override
    public void handleScheduCbabill(Date paySchedu) {
        BillOperation billOperation = BillOperation.allow;
        // 根据计划时间取出需要进行转账的账单
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("payType", 1);
        params.put("state", 1);
        params.put("paySchedu_time", paySchedu);
        List<Long> list = cbabillMapper.selectIdList(params);
        for (Long billId : list) {
            try {
                this.flowBill(billId, billOperation);
            } catch (Exception e) {
                logger.error("处理账单发生异常 billId=" + billId, e);
            }
        }
    }
    
    //@Cacheable("transferPoolLmt")
    public BigDecimal transferPoolLmt() {
        SysConfig cfg = iConfigMgr.getByCfgidAndCfgkey(CFG_CBA_CFG_ID, CFG_TRANSFER_POOL_LMT_KEY);
        if (cfg == null) {
            throw new RuntimeException("配置信息有误， 读取不到CBA配置");
        }
        String cfgVal = cfg.getCfgVal();
        BigDecimal transferPoolLmt = new BigDecimal(cfgVal);
        return transferPoolLmt;
    }

    
    @Cacheable("autoPayLmt")
    public BigDecimal loadAutoPayLmt() {
        SysConfig cfg = iConfigMgr.getByCfgidAndCfgkey(CFG_CBA_CFG_ID, CFG_AUTO_PAY_LMT_KEY);
        if (cfg == null) {
            throw new RuntimeException("配置信息有误， 读取不到CBA配置");
        }
        String cfgVal = cfg.getCfgVal();
        BigDecimal autoPayLmt = new BigDecimal(cfgVal);
        return autoPayLmt;
    }

    @Cacheable("cbaBatchLmt")
    public BigDecimal loadBatchLmt() {
        BigDecimal batchLmt = null;
        SysConfig cfg = iConfigMgr.getByCfgidAndCfgkey(CFG_CBA_CFG_ID, CFG_BATCH_LMT_KEY);
        if (cfg == null) {
            batchLmt = new BigDecimal(0);
            logger.warn("配置信息有误， 读取不到CBA配置中的单次最低限额");
        }
        String cfgVal = cfg.getCfgVal();
        batchLmt = new BigDecimal(cfgVal);
        return batchLmt;
    }

    /**
     * 加载CBA账户信息
     * @param use 用途
     * @return
     */
    @Cacheable("cbaacc")
    public Cbaacc loadCbaacc(byte use) {
        Cbaacc cbaacc = cbaaccMapper.selectByUse(use);
        if (cbaacc == null) {
            throw new RuntimeException("配置信息有误， 读取不到CBA账户信息");
        }
        return cbaacc;
    }

    @Cacheable("cbaVerifier")
    public SysUsers queryVerifier() {
        return verifierMapper.queryVerifier();
    }

    private boolean saveAlipayOrderBillId(List<AlipayOrder> orders, Long billId) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("orders", orders);
        param.put("billId", billId);
        int count = alipayOrderMapper.saveAlipayOrderBillId(param);
        return count > 0 ? true : false;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public Cbabill startAlipayOrderBill(List<AlipayOrder> orders, Business bus) throws Exception {
        Byte payEnable = bus.getPayEnable();
        if (payEnable != 1) {
            logger.warn("支付宝商家不支持联邦银行转账 bsId={}", bus.getId());
            return null;
        }
        logger.info("启动支付宝活动消费账单 bsId={}", bus.getId());
        Cbabill cbabill = new Cbabill();
        Cbaacc cbaAcc = null;
        cbabill.setBsType((byte) 5); // 支付宝活动订单 转账类型 廖江红添加
        // 设置收款方信息
        cbabill.setBsBsb(bus.getBsb());
        cbabill.setBsAcc(bus.getAccount());
        cbabill.setBsName(bus.getAccName());
        cbabill.setRemitter("supaytech");
        cbabill.setPayDesc("supaytech");
        cbabill.setPayType((byte) 1);
        cbaAcc = this.loadCbaacc((byte) 1);
        Double amount = 0d; // 总转账金额
        for (AlipayOrder o : orders) {
            amount += new BigDecimal(o.getAmount().toString()).subtract(new BigDecimal(o.getBrokerage().toString()))
                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        cbabill.setLodgementRef(String.format("%1$02d%2$016d", cbabill.getBsType(), orders.get(0).getId()));
        cbabill.setAmount(amount);
        BigDecimal autoPayLmt = loadAutoPayLmt();
        if (autoPayLmt.compareTo(new BigDecimal(cbabill.getAmount().toString())) < 0) { // 金额大于后台设置的转账金额 需要管理员审核
            cbabill.setPayType((byte) 2);
            cbaAcc = this.loadCbaacc((byte) 2);
            /** 人工转账短信通知审核员 */
            SysUsers verifier = queryVerifier();
            String content = "您好，授权人，您有一笔联邦银行的转账订单待审核，请及时到SUPAYTECH后台处理。" + Constants.SMS_LAST_STR;
            smsService.sendMessage(verifier.getPhone(), content);
        }
        // 设置付款方信息
        cbabill.setBsb(cbaAcc.getBsb());
        cbabill.setAcc(cbaAcc.getAcc());
        cbabill.setAccName(cbaAcc.getAccName());
        // 设置流程状态
        cbabill.setState((byte) 1);
        cbabill.setCrtime(DateUtil.getCurDate());
        cbabill.setUptime(DateUtil.getCurDate());
        // 保存到数据库
        if (cbabillMapper.insert(cbabill) != 1) {
            throw new RuntimeException("insert fail");
        }
        /** 保存billId进转账账单表中,并修改转账状态（transfer_status字段） */
        saveAlipayOrderBillId(orders, cbabill.getId());
        if (cbabill.getPayType() == 1) {
            if (bus.getCbapayType() == 1) {
                // 立即处理
                this.flowBill(cbabill.getId(), BillOperation.allow);
            }
        }
        return cbabill;
    }

    /**
     * 每月8号0点转账
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public Cbabill startAlipayOrderAll(List<AlipayOrder> orders, Business bus) throws Exception {
        Byte payEnable = bus.getPayEnable();
        if (payEnable != 1) {
            logger.warn("支付宝商家不支持联邦银行转账 bsId={}", bus.getId());
            return null;
        }
        logger.info("启动支付宝活动消费账单 bsId={}", bus.getId());
        Cbabill cbabill = new Cbabill();
        Cbaacc cbaAcc = null;
        cbabill.setBsType((byte) 5); // 支付宝活动订单 转账类型 廖江红添加
        // 设置收款方信息
        cbabill.setBsBsb(bus.getBsb());
        cbabill.setBsAcc(bus.getAccount());
        cbabill.setBsName(bus.getAccName());
        cbabill.setRemitter("supaytech");
        cbabill.setPayDesc("supaytech");
        cbabill.setPayType((byte) 1);
        cbaAcc = this.loadCbaacc((byte) 1);
        Double amount = 0d; // 总转账金额
        for (AlipayOrder o : orders) {
            amount += new BigDecimal(o.getAmount().toString()).subtract(new BigDecimal(o.getBrokerage().toString()))
                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        cbabill.setLodgementRef(String.format("%1$02d%2$016d", cbabill.getBsType(), orders.get(0).getId()));
        cbabill.setAmount(amount);
        // 设置付款方信息
        cbabill.setBsb(cbaAcc.getBsb());
        cbabill.setAcc(cbaAcc.getAcc());
        cbabill.setAccName(cbaAcc.getAccName());
        // 设置流程状态
        cbabill.setState((byte) 1);
        cbabill.setCrtime(DateUtil.getCurDate());
        cbabill.setUptime(DateUtil.getCurDate());
        cbabill.setDoneTime(DateUtil.getCurDate());
        // 保存到数据库
        if (cbabillMapper.insert(cbabill) != 1) {
            throw new RuntimeException("insert fail");
        }
        /** 保存billId进转账账单表中,并修改转账状态（transfer_status字段） */
        saveAlipayOrderBillId(orders, cbabill.getId());
        if (cbabill.getPayType() == 1) {
            if (bus.getCbapayType() == 1) {
                // 立即处理
                this.flowBill(cbabill.getId(), BillOperation.allow);
            }
        }
        return cbabill;
    }
    
    
    /**
     * 支付宝活动转账
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public Cbabill startAlipayTransfer(List<AlipayOrder> orders, Business bus, Double amount) throws Exception {
        Byte payEnable = bus.getPayEnable();
        if (payEnable != 1) {
            logger.warn("支付宝商家不支持联邦银行转账 bsId={}", bus.getId());
            return null;
        }
        logger.info("启动支付宝活动消费账单 bsId={}", bus.getId());
        logger.debug("需要转账的订单======" + orders.size() + "=========" + orders);
        Cbabill cbabill = new Cbabill();
        Cbaacc cbaAcc = null;
        cbabill.setBsType((byte) 5); // 支付宝活动订单 转账类型 廖江红添加
        // 设置收款方信息
        cbabill.setBsBsb(bus.getBsb());
        cbabill.setBsAcc(bus.getAccount());
        cbabill.setBsName(bus.getAccName());
        cbabill.setRemitter("supaytech");
        cbabill.setPayDesc("supaytech");
        cbabill.setPayType((byte) 1);
        cbaAcc = this.loadCbaacc((byte) 1);
        cbabill.setLodgementRef(String.format("%1$02d%2$016d", cbabill.getBsType(), orders.get(0).getId()));
        cbabill.setAmount(amount);
        // 设置付款方信息
        cbabill.setBsb(cbaAcc.getBsb());
        cbabill.setAcc(cbaAcc.getAcc());
        cbabill.setAccName(cbaAcc.getAccName());
        // 设置流程状态
        cbabill.setState((byte) 1);
        cbabill.setCrtime(DateUtil.getCurDate());
        cbabill.setUptime(DateUtil.getCurDate());
        cbabill.setDoneTime(DateUtil.getCurDate());
        // 保存到数据库
        if (cbabillMapper.insert(cbabill) != 1) {
            throw new RuntimeException("insert fail");
        }
        /** 保存billId进转账账单表中,并修改转账状态（transfer_status字段） */
        saveAlipayOrderBillId(orders, cbabill.getId());
        if (cbabill.getPayType() == 1) {
            if (bus.getCbapayType() == 1) {
                // 立即处理
                this.flowBill(cbabill.getId(), BillOperation.allow);
            }
        }
        return cbabill;
    }
    

    private void smsAlipayBusiness(Cbabill bill) {
        Business business = cbabillMapper.getAlipayBusinessByBill(bill.getId());
        if (business == null) {
            logger.warn("联邦银行转账成功向支付宝商家发送短信时,business为空,billId=" + bill.getId());
        } else {
            /** 向商家发送短信 */
            if (business.getPhone() != null && !"".equals(business.getPhone()) && !"".equals(business.getShopName())
                    && business.getShopName() != null) {
                String content = "[" + business.getShopName() + "]" + " Please be advised you have AU $" + bill.getAmount()
                        + " through CBA transfer to your,please check. http://mmobile.e-swipe.com.au/ . " + Constants.SMS_LAST_STR;
                smsService.sendBatchEventMessage(business.getPhone(), business.getPhone2(), business.getPhone3(), business.getPhone4(),
                        business.getPhone5(), content);
            }
        }
    }


    @Override
    public int updateIsActive(Map<String, Object> map) {
        return cbabillMapper.updateIsActive(map);
    }

    /**
     * 支付宝每天0点定时批量转账
     */
	@Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
	public List<Cbabill> startAlipayTransferBatch(List<Business> list) throws Exception {
		List<Cbabill> billList = new ArrayList<Cbabill>();
		String lodgementRef = "";
		Map<String, Object> map = new HashMap<String, Object>();
    	map.put("bsId", list.get(0).getId());
	    List<AlipayOrder> ordersfirst = alipayOrderMapper.transferAlipayOrderAll(map);
	    Byte bsType =5;
	    lodgementRef = String.format("%1$02d%2$016d", bsType, ordersfirst.get(0).getId());
        for(Business bus:list){
        	//查询对应商家的未转账的订单
            Double amount = 0d; // 总转账金额
            map.clear();
        	map.put("bsId", bus.getId());
		    List<AlipayOrder> orders = alipayOrderMapper.transferAlipayOrderAll(map);
		    if (!orders.isEmpty()) {
		        for (AlipayOrder o : orders) {    //计算转账总金额
		            amount += new BigDecimal(o.getAmount().toString()).subtract(new BigDecimal(o.getBrokerage().toString()))
		                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		        }
		    }
	        Cbabill cbabill = new Cbabill();
	        Cbaacc cbaAcc = null;
	        cbabill.setBsType((byte) 5); // 支付宝活动订单 转账类型 廖江红添加
	        // 设置收款方信息
	        cbabill.setBsBsb(bus.getBsb());
	        cbabill.setBsAcc(bus.getAccount());
	        cbabill.setBsName(bus.getAccName());
	        cbabill.setRemitter("supaytech");
	        cbabill.setPayDesc("supaytech");
	        cbabill.setPayType((byte) 1);
	        cbaAcc = this.loadCbaacc((byte) 1);
	        cbabill.setLodgementRef(lodgementRef);
	        cbabill.setAmount(amount);
	        // 设置付款方信息
	        cbabill.setBsb(cbaAcc.getBsb());
	        cbabill.setAcc(cbaAcc.getAcc());
	        cbabill.setAccName(cbaAcc.getAccName());
	        // 设置流程状态
	        cbabill.setState((byte) 1);
	        cbabill.setCrtime(DateUtil.getCurDate());
	        cbabill.setUptime(DateUtil.getCurDate());
	        cbabill.setDoneTime(DateUtil.getCurDate());
	        // 保存到数据库
	        if (cbabillMapper.insert(cbabill) != 1) {
	            throw new RuntimeException("insert fail");
	        }
	        /** 保存billId进转账账单表中,并修改转账状态（transfer_status字段） */
	        saveAlipayOrderBillId(orders, cbabill.getId());
	        //将转账信息批量
	        billList.add(cbabill);
        }
        logger.debug("需要转账的账单列表为：===================="+billList.size());
        // 立即处理
        this.flowBill(billList, BillOperation.allow);
        return billList;
    }

	

    /**
	 * 批量转账开始
	 */
	@Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public List<Cbabill> flowBill(List<Cbabill> billList, BillOperation billOperation) throws Exception {
		List<Cbabill> cbaList = new ArrayList<Cbabill>();
		for(Cbabill bill:billList){
	        logger.debug("联邦银行向商家转账开始==========================================");
	        // 为了保证每一笔订单的数据完整性，必须保证线程同步， 在取数据时使用悲观锁（select * from tb_cbabill where id = ? for update），提交后下一个事务才能进行操作
	        Cbabill cbabill = cbabillMapper.selectByIdForUpdate(bill.getId());
	        //Byte bsType = cbabill.getBsType();
	        // 流转状态
	        cbabill = billOperation.flow(cbabill);
	        cbabill.setUptime(DateUtil.getCurDate());
	        byte state = cbabill.getState();
	        //Byte payType = cbabill.getPayType();
	        if (state == 4) {
	            // 账单流程结束
	            cbabill.setDoneTime(DateUtil.getCurDate());
	            /** 转账成功时，商家订单需要向商家发送push通知 2015-8-24 廖江红添加活动转账向商家push */
	        }
	        // 更新到数据库
	        if (cbabillMapper.updateByPrimaryKeySelective(cbabill) != 1) {
	            throw new RuntimeException("save fail");
	        }
	        cbaList.add(cbabill);
	    }
        if(cbaList.get(0).getState()==3){
	        // 向联邦银行提交转账请求
	        List<CommbankPay> commbankPay = commBankService.createPaymentBatch(cbaList);
	        commBankService.postPaymentBatch(commbankPay);
        }
        if (cbaList.get(0).getState() == 4) {// 转账成功后给对应商家发送短信1.7.0版本需求    
        	for (Cbabill cbabill:cbaList) {
	            logger.debug("联邦银行转成功向支付宝商家发送短信 billid=" + cbabill.getId());
	            //转账成功后向app推送消息，然后app给商家发送短信
	            List<AlipayOrder> orders = alipayOrderMapper.selectAlipayOrderByBillId(cbabill.getId());
	            logger.debug("需要转账的订单======" + orders.size() + "=========");
	            Business business = cbabillMapper.getAlipayBusinessByBill(cbabill.getId());
	            logger.debug("向商家发送短信开始============"+(business != null && orders!=null && orders.size()>0));
	            if (business != null && orders!=null && orders.size()>0) {
	                /** 向商家发送短信 */
	                if (business.getPhone2() != null && !"".equals(business.getPhone2()) && !"".equals(business.getShopName())
	                        && business.getShopName() != null) {
	                    // 修改转账短信推送信息
	                    for (int i = 0; i < orders.size(); i++) {
	                        Map<String, Object> map = new HashMap<String, Object>();
	                        map.put("orderNo", orders.get(i).getOrderNo());
	                        map.put("billId", cbabill.getId());
	                        map.put("pushtCount", 1);   //转账推送次数
	                        map.put("transferpushSendTime", DateUtil.getDatetime());
	                        // 根据订单号修改短信日志信息
	                        logger.debug("根据订单号修改短信日志信息======" + orders.size() + "=========" + orders.get(i).getOrderNo());
	                        pushSmsLogMapper.updBillIdAndTstimeByOrderNo(map);
	                    }
	                    String content = "[" + business.getShopName() + "]" + " Please be advised you have AU $" + cbabill.getAmount()
	                            + " through CBA transfer to your,please check. http://mmobile.supaytech.com.au/ . " + Constants.SMS_LAST_STR;
	                    StringBuffer sb = new StringBuffer(business.getPhone2()+"");
	                    String sendPhone = "";
	                    if (StringUtils.isNotEmpty(business.getPhone3())) {// 大陆商家
	                        sb.append("|").append(business.getPhone3());
	                        if (StringUtils.isNotEmpty(business.getPhone4())) {
	                            sb.append("|").append(business.getPhone4());
	                        }
	                        if (StringUtils.isNotEmpty(business.getPhone5())) {
	                            sb.append("|").append(business.getPhone5());
	                        }
	                    }
	                    sendPhone = sb.toString();
	                    logger.debug("转账第一次需要推送的号码===="+sendPhone);
	                    pushSmsService.smspush(sendPhone, String.valueOf(cbabill.getId()),content,2);
	                }
	            }  
        	}
        }
        logger.info("账单状态完成流转 billOperation={}", billOperation);
        return cbaList;
    }
	
}
    
  

  
